﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Anagramme.Model;
using System.ApplicationModel;
using System.ApplicationModel.Composition;
using System.Activities;
using Anagramme.Views;
using System.Activities.Hosting;
using System.Threading;
using System.Windows;
using System.ComponentModel.Composition.Hosting;
using Anagramme.Services;

namespace Anagramme.ViewModels
{
    /// <summary>
    /// ViewModel de la fenêtre principale
    /// </summary>
    [ViewModelExport(Modules.Main)]
    public class MainWindowViewModel : ViewModelBase
    {
        #region Constructors
        /// <summary>
        /// Crée une nouvelle instance de la classe <see cref="MainWindowViewModel"/>
        /// </summary>
        public MainWindowViewModel()
        {
            ApplicationServices.ViewWorkflow.Run();

            WeakEventManager<IViewWorkflowManager, ViewWorkflowEventArgs>.AddHandler(ApplicationServices.ViewWorkflow, "MessageReceived", this.ViewWorkflow_MessageReceived);
        }
        #endregion

        #region Fields
        private object _content;
        #endregion

        #region Properties
        /// <summary>
        /// Obtient ou définit le contenu de la fenêtre principale
        /// </summary>
        public object Content
        {
            get { return this._content; }
            set
            {
                if (!object.ReferenceEquals(this._content, value))
                {
                    base.RaisePropertyChanging();
                    this._content = value;
                    base.TraceAssign(value);
                    base.RaisePropertyChanged();
                }
            }
        }
        #endregion

        #region Event Handlers
        private void ViewWorkflow_MessageReceived(object sender, ViewWorkflowEventArgs e)
        {
            ApplicationServices.Trace.TraceMethodEntering();

            System.ApplicationModel.Application.Current.Dispatcher.BeginInvoke(new Action(() => this.Content = ApplicationServices.Container.GetView(e.View)));

            ApplicationServices.Trace.TraceMethodLeaving();
        }
        #endregion
    }
}
